<?php
/**
 * Created by PhpStorm.
 * User: fengzi
 * Date: 2019/6/3
 * Time: 14:34
 */

namespace app\vehicle\logic;
use think\Db;
use app\vehicle\model\Config;
use app\vehicle\model\Order;
use app\vehicle\model\Driver;
use app\vehicle\model\CouponUser;
use app\vehicle\model\User;
use app\vehicle\model\UserIntegral;
use app\vehicle\logic\TplLogic;
use app\vehicle\logic\tokenLogic;
class orderLogic
{
    public function shopOrderList($user_id,$data){
        $where['status'] = $data['status'];
        $list = Order::where('shop_id','eq',$data['shop_id'])->where($where)->order('create_time desc')->page($data['page'],10)->select();
        return $list;
    }

    public function orderList($user_id,$data){
        $where['status'] = $data['status'];
        $list = Order::where('user_id','eq',$user_id)->where($where)->order('create_time desc')->page($data['page'],10)->select();
        return $list;
    }
    public function getOrderInfo($data){
        $info = Order::where('id','eq',$data['order_id'])->field('*,status as sid')->find();
        $info['coupon'] = CouponUser::where('user_id','eq',$info['user_id'])->where('use_min_price','lt',$info['order_price'])->where('end_time','gt',time())->where('status','eq',0)->select();
        if($data['coupon_id']){
            $coupon = CouponUser::where('id','eq',$data['coupon_id'])->where('status','eq',0)->find();
            if($coupon['id']){
                $info['pay_price'] = $info['order_price']-$coupon['coupon_price'];
                $info['coupon_price'] = $coupon['coupon_price'];
            }
        }else{
            $info['pay_price'] = $info['order_price'];
        }
        $info['driver'] = Driver::where('id','eq',$info['driver_id'])->find();
        return $info;
    }
    public function cancleOrder($data,$user_id){
        $info = Order::where('id','eq',$data['order_id'])->field('*,status as sid')->find();
        if($info['sid']>1){
            return '操作失敗';
        }
        Order::where('id','eq',$data['order_id'])->update(['status'=>3,'is_shop_read'=>0,'is_read'=>0,'update_time'=>time()]);
        return 1;
    }
    public function completeOrder($data,$user_id){
        $user = User::get($user_id);
        $info = Order::where('id','eq',$data['order_id'])->field('*,status as sid')->find();
        if(!$info['driver_id']){
            return '該訂單未分配司機';
        }
        $driver = Driver::get($info['driver_id']);
        if($driver['mobile']!=$user['mobile']){
            return '您不是本次的操作司機';
        }
        if($info['sid']==2){
            return '該訂單已確認';
        }
        Db::startTrans();
        try {
            $coupon_price = 0;
            if($data['coupon_id']){
                $coupon = CouponUser::where('id','eq',$data['coupon_id'])->where('status','eq',0)->find();
                if($coupon['id']){
                    $coupon_price = $coupon['coupon_price'];
                    CouponUser::where('id','eq',$coupon['id'])->update(['status'=>1]);
                }

            }
            $info['pay_price'] = $info['order_price']-$coupon_price;
            Order::where('id','eq',$data['order_id'])->update(['status'=>2,'pay_price'=>$info['pay_price'],'coupon_price'=>$coupon_price]);
            $config_integral = Config::where('key','eq','sys_name')->value('value');
            $integral = $info['pay_price']*$config_integral/100;
            if($integral) {
                $insert['user_id'] = $info['user_id'];
                $insert['order_id'] = $info['id'];
                $insert['create_time'] = time();
                $insert['integral'] = $config_integral;
                UserIntegral::insert($insert);
                User::where('id','eq',$info['user_id'])->setInc('integral',intval($integral));
                if($info['shop_id']){
                    $insert['user_id'] = $info['shop_id'];
                    $insert['order_id'] = $info['id'];
                    $insert['create_time'] = time();
                    $insert['integral'] = $config_integral;
                    UserIntegral::insert($insert);
                    User::where('id','eq',$info['shop_id'])->setInc('integral',intval($integral));
                }
            }
            Db::commit();
            return 1;
        } catch (\Exception $e) {
            // 回滚事务
            Db::rollback();
            return 0;
        }
    }
    public function setOrder($data,$user_id){
        Db::startTrans();
        try {
            $user = User::get($user_id);
            if($data['shop_id'] && $data['shop_id']!=$user_id){
                $insert['shop_id'] = $data['shop_id'];
            }
            $insert['user_id'] = $user_id;
            $insert['order_sn'] = date('YmdHi').rand(1000,9999);
            $insert['start_city'] = $data['start_city'];
            $insert['end_city'] = $data['end_city'];
            $insert['use_time'] = strtotime($data['use_time']);
            $insert['site'] = $data['site'];
            $insert['number'] = $data['number'];
            $insert['realname'] = $data['realname'];
            $insert['mobile'] = $data['mobile'];
            $insert['remark'] = $data['remark'].$data['str_date'].' '.$data['str_time'];
            $insert['form_id'] = $data['form_id'];
            $insert['back_time'] = strtotime($data['back_time']);
            $insert['back_site'] = $data['back_site'];
            $insert['create_time'] = time();
            $order_id = Order::insertGetId($insert);
            TplLogic::createOrder($user['open_id'],$data['form_id'],$insert['order_sn'],$order_id);
            Db::commit();
            return 1;
        } catch (\Exception $e) {
            // 回滚事务
            Db::rollback();
            return 0;
        }
    }
}